package com.yyjj.hangqing.kline;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.entity.BaseTResp;
import com.yyjj.hangqing.entity.CoinDetailEntity;
import com.yyjj.hangqing.kline.adapter.KlineSimpleFragmentPagerAdapter;

import java.util.ArrayList;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.view.SpaceItemDecoration;

/**
 * Anthor:NiceWind
 * Time:2019/6/23
 * Desc:The ladder is real, only the climb is all.
 */
@Route( path = "/hq/kline/coin")
public class CoinKLineActivity extends BaseTitleActivity{
    String symbol;
    private TitleBarView mTitleBarHeadFastLib;
    private android.widget.TextView mTvPrice;
    private android.widget.TextView mTvDifferencePrice;
    private android.widget.TextView mTvPricePrse;
    private android.widget.TextView mTvTime;
    private android.widget.TextView mTvNewOpen;
    private android.widget.TextView mTvAmplitude;
    private android.widget.TextView mTvMaxPrice;
    private android.widget.TextView mTvTvMinPrice;
    private android.widget.TextView mTvInventory;
    private android.widget.TextView mTvYesterdayTheSettlement;
    private android.support.design.widget.TabLayout mTabKline;
    private NoScrollViewPager mViewPagerKline;
    private android.widget.TextView mBtnKlineFull;
    private android.widget.TextView mTvAddProduct;

    TitleBarView titleBar;
    private android.support.v7.widget.RecyclerView mRecyclerview;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        symbol = getIntent().getStringExtra("symbol");
        this.titleBar =titleBar;
        titleBar.setTitleMainTextMarquee(true)
                .setTitleMainText("K线图");
    }

    @Override
    public int getContentLayout() {
        return R.layout.hq_activity_kline_main_coin;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        mTitleBarHeadFastLib = (TitleBarView) findViewById(R.id.titleBar_headFastLib);
        mTvPrice = (TextView) findViewById(R.id.tv_price);
        mTvDifferencePrice = (TextView) findViewById(R.id.tv_difference_price);
        mTvPricePrse = (TextView) findViewById(R.id.tv_price_prse);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvNewOpen = (TextView) findViewById(R.id.tv_new_open);
        mTvAmplitude = (TextView) findViewById(R.id.tv_amplitude);
        mTvMaxPrice = (TextView) findViewById(R.id.tv_max_price);
        mTvTvMinPrice = (TextView) findViewById(R.id.tv_tv_min_price);
        mTvInventory = (TextView) findViewById(R.id.tv_inventory);
        mTvYesterdayTheSettlement = (TextView) findViewById(R.id.tv_yesterday_the_settlement);
        mTabKline = (TabLayout) findViewById(R.id.tab_kline);
        mViewPagerKline = (NoScrollViewPager) findViewById(R.id.view_pager_kline);
        mBtnKlineFull = (TextView) findViewById(R.id.btn_kline_full);
        mTvAddProduct = (TextView) findViewById(R.id.tv_add_product);
        mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview);

        mRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerview.addItemDecoration(new SpaceItemDecoration(1));
//        mRecyclerview.setAdapter();
//        getSupportFragmentManager().beginTransaction().replace(R.id.ll_zixun, ServiceFactory.getInstance().getZixunService().newArticalFragment(null)).commit();


        initkLine();
        requestCoin();
//        requestZixun();
    }

    private void requestCoin(){
//        Double differencePrice = Double.parseDouble(item.getOpen()) - Double.parseDouble(item.getTrade());
//        tvPrice.setText(item.getTrade());
//        tvDifferencePrice.setText(DataUtils.getPercentValue(differencePrice) + "");
//        tvPricePrse.setText(DataUtils.getPercentValue(Double.parseDouble(item.getPrevsettlement())) + "%");
//        tvTime.setText(item.getDate_time());
//        tvNewOpen.setText("今开：" + item.getOpen());
//        //振幅
//        tvAmplitude.setText("振幅：" + DataUtils.getPercentValue(Double.parseDouble(item.getChangepercent())) + "%");
//        //最高价
//        tvMaxPrice.setText("最高：" + item.getHight());
//        //最低价
//        tvTvMinPrice.setText("最低：" + item.getLow());
//        //持有量
//        tvInventory.setText("持有量：" + item.getVolume());
//        //昨结算
//        tvYesterdayTheSettlement.setText("昨结算：" + item.getSettlement());

//        https://data.fk7h.com/yapi/bizhihui/quotes?size=50&page=1&sort=&order=1&symbol=NEXT
        ViseHttp.GET("/yapi/bizhihui/quotes")
                .baseUrl(ApiConstant.BASE_URL)
                .addParam("symbol",symbol)
                .request(new ACallback<BaseTResp<ArrayList<CoinDetailEntity>>>() {
                    @Override
                    public void onSuccess(BaseTResp<ArrayList<CoinDetailEntity>> data) {
                        if(data.code==1&&data.data!=null&&data.data.size()>0){
                            CoinDetailEntity entity = data.data.get(0);
                            titleBar.setTitleMainText(DataUtils.isEmpty(entity.getCn())?entity.getSymbol():entity.getCn());
                            mTvPrice.setText(entity.getId());
                            mTvTime.setText(DataUtils.getRoundUp(entity.getPrice_cny(),3));
                            mTvDifferencePrice.setText(entity.getPercent_change_24h_str());
                            mTvNewOpen.setText("今开: "+ DataUtils.getRoundUp(entity.getPrice_cny(),3));
                            mTvAmplitude.setText("涨跌幅: "+entity.getPercent_change_24h_str());
                            mTvInventory.setText("市值: "+entity.getMarket_cap_cny_str());
                            mTvMaxPrice.setText("流通量:" + entity.getAvailable_supply_str());
                            mTvTvMinPrice.setText("七日波动: " + DataUtils.format2Decimals(entity.getPercent_change_7d())+"%");
                            mTvYesterdayTheSettlement.setText("小时波动: "+entity.getPercent_change_1h_str());
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    /**    5	|	默认值	|	5分钟线
     15	|		|	15分钟线
     30	|		|	30分钟线
     60	|		|	1小时线
     360	|		|	6小时线
     1440	|		|	日线
     10080	|		|	周线*/
    private void initkLine(){

        Fragment[] fragments;
        String[] titles;

        fragments = new Fragment[]{
                KLineChartCoinFragment.newInstance(symbol, KLineChartCoinFragment.TYPE_DAY, "1440","/yapi/wankenet/get_line"),
                KLineChartCoinFragment.newInstance(symbol, KLineChartCoinFragment.TYPE_5_MINUTE, "5","/yapi/wankenet/get_line"),
                KLineChartCoinFragment.newInstance(symbol, KLineChartCoinFragment.TYPE_15_MINUTE, "15","/yapi/wankenet/get_line"),
                KLineChartCoinFragment.newInstance(symbol, KLineChartCoinFragment.TYPE_30_MINUTE, "30","/yapi/wankenet/get_line"),
                KLineChartCoinFragment.newInstance(symbol, KLineChartCoinFragment.TYPE_30_MINUTE, "60","/yapi/wankenet/get_line")
        };
        titles = new String[]{"日K", "5分", "15分", "30分", "60分"};
        mViewPagerKline.setOffscreenPageLimit(fragments.length);
        mViewPagerKline.setAdapter(new KlineSimpleFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles));
        mTabKline.setupWithViewPager(mViewPagerKline);

    }
}
